package com.runfast.waimai.web.controller.business;

import com.google.common.base.Joiner;
import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastBusinessCommentService;
import com.runfast.waimai.utils.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.*;

/**
 * ${DESCRIPTION}  评论商家
 *
 * @author chenzheng
 * @create 2018-05-09 9:22
 **/
@RestController("businessCommentController")
@RequestMapping("/server/businessComment")
public class BusinessCommentController {
    @Autowired
    private RunfastBusinessCommentService runfastBusinessCommentService;

    /**
     * 评论商家列表带分页的
     */
    @GetMapping("/list")
    public Result list(@PageableDefault Pageable pageable, RunfastBusinessComment comment) throws ParseException {
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        RunfastBusinessAccounts accounts = SessionContext.getRunfastBusinessAccounts();
        Map<Object,Object> map=new HashMap<Object, Object>();
        Map<Object,Object> map1=new HashMap<Object, Object>();
        if(comment!=null){
            if(comment.getScore()!=null){
                map1.put("score",comment.getScore());
            }
            if(StringUtils.isNotBlank(comment.getDate())){
                List<String> result = Arrays.asList(comment.getDate().split(","));
                map1.put("startTime",TimeUtil.getDate(result.get(0)));
                map1.put("endTime",TimeUtil.getDate(result.get(1)));
            }
            if(StringUtils.isNotBlank(comment.getBusinessname())){
                map1.put("businessName",comment.getBusinessname());
            }
            if(StringUtils.isNotBlank(comment.getDelicername())){
                map1.put("delicername",comment.getDelicername());
            }
            if(StringUtils.isNotBlank( comment.getUsername())){
                map1.put("userName",comment.getUsername());
            }
        }
        if(admin!=null && admin.getType()==3 && accounts!=null){
            //商家帐号登录----只能看自己家的

        }else if(admin!=null && admin.getType()==2 && agentAccounts!=null){
            map1.put("agentIds",agentAccounts.getAgentid());
        }else if(admin !=null && admin.getType()==1){
            //后台管理员---查看全部的
            /**代理商多选*/
            if(StringUtils.isNotBlank(comment.getAgentIds())){
                map1.put("agentIds",comment.getAgentIds());
            }
           /* else{
                if(admin.getAgents().size()>0){
                    map1.put("agentIds",Joiner.on(",").join(admin.getAgents()));
                }
            }*/
        }
        map1.put("pageable",pageable);
        List<RunfastBusinessComment> list=runfastBusinessCommentService.selectByMapWithPageable(map1);
        Integer total=runfastBusinessCommentService.countByMap(map1);
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /**代理商回复用户评价*/
    @GetMapping("/setComment")
    public Result setComment(Integer id ,String replydrivercomment){
        if(id!=null && StringUtils.isNotBlank(replydrivercomment)){
            RunfastBusinessComment comment=runfastBusinessCommentService.selectByPrimaryKey(id);
            comment.setReplydrivercomment(replydrivercomment);
            runfastBusinessCommentService.updateByPrimaryKeySelective(comment);
        }
        return Result.ok(null);
    }

    /**
     * 评论商家列表
     */
   /* @GetMapping("/select")
    @RequiresPermissions("server:businessComment:select")
    public Result select(){
        List<RunfastGoodsSellType> list = runfastGoodsSellTypeService.selectByExample(null);
        return Result.ok(null,list);
    }*/

     /**
      * 评论商家信息
      */
    @GetMapping("/info")
    public Result info(Integer id){
        if(id==null){return Result.fail(null,"id不能为空！");}
        RunfastBusinessComment comment = runfastBusinessCommentService.selectByPrimaryKey(id);
        return Result.ok(null,comment);
    }

    /**
     * 保存评论商家
     */
    @PostMapping("/save")
    //@RequiresPermissions("server:businessComment:save")
    public Result save(@RequestBody RunfastBusinessComment comment){
        comment.setCreatetime(new Date());
        runfastBusinessCommentService.insert(comment);
        return Result.ok(null);
    }

    /**
     * 修改评论商家
     */
    @PostMapping("/update")
    //@RequiresPermissions("server:businessComment:update")
    public Result update(@RequestBody RunfastBusinessComment comment){
        runfastBusinessCommentService.updateByPrimaryKeySelective(comment);
        return Result.ok(null);
    }

    /**
     * 删除评论商家
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        if(id!=null){
            RunfastBusinessComment runfastBusinessComment=new RunfastBusinessComment();
            runfastBusinessComment.setId(id);
            runfastBusinessComment.setDeleted(true);
            runfastBusinessCommentService.updateByPrimaryKeySelective(runfastBusinessComment);
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

}
